package com.ruoyi.jpjstb.controller.cost;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.jpjstb.domain.common.Cost;
import com.ruoyi.jpjstb.domain.common.Dept;
import com.ruoyi.jpjstb.domain.common.Month;
import com.ruoyi.jpjstb.service.CostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ruoyi
 * 测试
 */
@Controller
@RequestMapping("/cost")
public class CostController extends BaseController {
    @Autowired
    CostService costService;

    private String prefix = "hr/demo/cost";


    @GetMapping()
    public String hour(ModelMap mmap, HttpServletRequest request) {


        List<Dept> deptinfos=costService.findDept(null);
        mmap.put("deptinfos",deptinfos);

        List<Month> monthinfos=costService.findmonth(null);
        mmap.put("monthinfos",monthinfos);

        return prefix + "/cost";
    }

    //查询列表
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Cost cost)
    {
        startPage();
        List<Cost> list =costService.selectCostList(cost);
        return getDataTable(list);
    }


    //删除
    /**
     *
     */
    @Log(title = "薪资明细", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(costService.deleteCostByIds(ids));
    }


    //新增保存
    /**
     *
     */
    @Log(title = "薪资明细", businessType = BusinessType.INSERT)
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Cost cost)
    {
        return toAjax(costService.insertCost(cost));
    }

    @GetMapping("/add")
    public String add(ModelMap mmap, HttpServletRequest request) {

        List<Month> monthinfos=costService.findmonth(null);
        mmap.put("monthinfos",monthinfos);


        List<Dept> deptinfos=costService.findDept(null);
        mmap.put("deptinfos",deptinfos);
        return prefix + "/add";
    }


    //修改
    /**
     * 修改
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        //根据主键查询单条数据
        Cost hrCost = costService.selectCostById(id);
        mmap.put("hrCost", hrCost);

        List<Month> monthinfos=costService.findmonth(null);
        mmap.put("monthinfos",monthinfos);

        List<Dept> deptinfos=costService.findDept(null);
        mmap.put("deptinfos",deptinfos);
        return prefix + "/edit";
    }

    /**
     * 修改保存
     */
    @Log(title = "小时填报", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Cost cost) {
        return toAjax(costService.updateCost(cost));
    }


    /**
     * 导出
     */
    @Log(title = "薪资明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Cost cost)
    {
        List<Cost> list =costService.selectCostList(cost);
        ExcelUtil<Cost> util = new ExcelUtil<Cost>(Cost.class);
        return util.exportExcel(list, "cost");
    }

    /**
     * 导入Excel
     * author : hxy
     */
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception {
        return toAjax(costService.importData(file));
    }




}